﻿using System;
using System.Windows.Forms;
using PiFramework;
namespace PiControls.Controls.DriveUI.ModbusUI
{
    public partial class ModbusUi : UserControl
    {
        public ModbusUi()
        {
            InitializeComponent();
        }

        public void LoadConfig()
        {

            string ip = PModbusTCP.Instance.getIP();
            int port = PModbusTCP.Instance.getPort();
            this.textBox_ip.Text = ip;
            this.textBox_port.Text = port.ToString();

            if (PModbusTCP.Instance.IsConnected)
            {
                this.groupBox_Read.Enabled = true;
                this.groupBox_Write.Enabled = true;
                return;
            }
            this.groupBox_Read.Enabled = false;
            this.groupBox_Write.Enabled = false;




        }
        private void ModbusUi_Load(object sender, EventArgs e)
        {

        }

        private void button_Open_Click(object sender, EventArgs e)
        {
            string ip = this.textBox_ip.Text.Trim();
            int port = int.Parse(this.textBox_port.Text.Trim());
            PModbusTCP.Instance.Close();
            PModbusTCP.Instance.setIpAndPort(ip, port);
            bool isConnected = PModbusTCP.Instance.Connect();
            if (!isConnected)
            {
                MessageBox.Show("连接失败！");
                return;
            }

            this.groupBox_Read.Enabled = true;
            this.groupBox_Write.Enabled = true;
            this.button_Open.Enabled = false;
            this.button_Close.Enabled = true;

        }

        private void button_ShourR_Click(object sender, EventArgs e)
        {

            string ReadAddressText = this.textBox_ReadAddress.Text.Trim();
            ushort value = PModbusTCP.Instance.ReadUshort(stringToushort(ReadAddressText));
            string currentTime = DateTime.Now.ToString("mm:ss:ff");
            string str = currentTime + "[" + ReadAddressText + "] " + value + "\n";
            this.textBox_ReadRes.AppendText(str);
        }

        private ushort stringToushort(string str)
        {
            ushort ReadAddress;
            if (ushort.TryParse(str, out ReadAddress))
            {
                return ReadAddress;
            }
            MessageBox.Show("请输入有效的地址！");
            return 0;
        }

        private void button_ShourW_Click(object sender, EventArgs e)
        {
            string WriteAddressText = this.textBox_WriteAddress.Text.Trim();
            string WriteValueText = this.textBox_WriteValue.Text.Trim();
            ushort WriteValue = stringToushort(WriteValueText);
            bool isWrite = PModbusTCP.Instance.WriteUshort(stringToushort(WriteAddressText), WriteValue);
            if (isWrite)
            {
                string currentTime = DateTime.Now.ToString("mm:ss:ff");
                string str = currentTime + "[" + WriteAddressText + "] " + WriteValueText + "\n";
                this.textBox_WriteRes.AppendText(str);
            }
            else
            {
                MessageBox.Show("写入失败！");
            }
        }

        private void button_IntR_Click(object sender, EventArgs e)
        {
            string ReadAddressText = this.textBox_ReadAddress.Text.Trim();
            int value = PModbusTCP.Instance.ReadInt32(stringToushort(ReadAddressText));
            string currentTime = DateTime.Now.ToString("mm:ss:ff");
            string str = currentTime + "[" + ReadAddressText + "] " + value + "\n";
            this.textBox_ReadRes.AppendText(str);
        }

        private void button_IntW_Click(object sender, EventArgs e)
        {
            string WriteAddressText = this.textBox_WriteAddress.Text.Trim();
            string WriteValueText = this.textBox_WriteValue.Text.Trim();
            int WriteValue = int.Parse(WriteValueText);
            bool isWrite = PModbusTCP.Instance.WriteInt32(stringToushort(WriteAddressText), WriteValue);
            if (isWrite)
            {
                string currentTime = DateTime.Now.ToString("mm:ss:ff");
                string str = currentTime + "[" + WriteAddressText + "] " + WriteValueText + "\n";
                this.textBox_WriteRes.AppendText(str);
            }
            else
            {
                MessageBox.Show("写入失败！");
            }
        }

        private void button_doubleR_Click(object sender, EventArgs e)
        {
            string ReadAddressText = this.textBox_ReadAddress.Text.Trim();
            double value = PModbusTCP.Instance.ReadDouble(stringToushort(ReadAddressText));
            string currentTime = DateTime.Now.ToString("mm:ss:ff");
            string str = currentTime + "[" + ReadAddressText + "] " + value + "\n";
            this.textBox_ReadRes.AppendText(str);
        }

        private void button_doubleW_Click(object sender, EventArgs e)
        {
            string WriteAddressText = this.textBox_WriteAddress.Text.Trim();
            string WriteValueText = this.textBox_WriteValue.Text.Trim();
            double WriteValue = double.Parse(WriteValueText);
            bool isWrite = PModbusTCP.Instance.WriteDouble(stringToushort(WriteAddressText), WriteValue);
            if (isWrite)
            {
                string currentTime = DateTime.Now.ToString("mm:ss:ff");
                string str = currentTime + "[" + WriteAddressText + "] " + WriteValueText + "\n";
                this.textBox_WriteRes.AppendText(str);
            }
            else
            {
                MessageBox.Show("写入失败！");
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            PModbusTCP.Instance.Close();
            this.button_Open.Enabled = true;
            this.button_Close.Enabled = false;
            this.groupBox_Read.Enabled = false;
            this.groupBox_Write.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ip = PModbusTCP.Instance.getIP();
            int port = PModbusTCP.Instance.getPort();
            Console.WriteLine("ip地址 " + ip);
            Console.WriteLine("端口号 " + port);
        }
    }
}
